package cn.ren.service;

import cn.ren.constant.RedisConstant;
import cn.ren.dao.AddressDao;
import cn.ren.entity.PageResult;
import cn.ren.pojo.Address;
import cn.ren.pojo.Setmeal;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired //注入jedis连接池
    private JedisPool jedisPool;
    @Autowired
    private AddressDao addressDao;
    @Override
    public void add(Address address) {
        addressDao.add(address);

        String img = address.getImg();
        if(img!=null){
            //将文件名存到redis保存有用数据的set里
            jedisPool.getResource().sadd(
                    RedisConstant.SETMEAL_PIC_DB_RESOURCES,address.getImg());
        }
    }

    @Override
    public PageResult queryPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = addressDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void edit(Address address) {
        addressDao.edit(address);
    }

    @Override
    public void del(Integer id) {
        addressDao.del(id);
    }
}
